package com.yanqu.road.dao.impl.activity.newserverwelfare;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.newserverwelfare.NewServerWelfareReward;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class NewServerWelfareRewardDataDaoImpl extends TempDataObjectDao<NewServerWelfareReward> {

    public static String getNewServerWelfareRewardSql = "select * from `t_u_activity_new_server_welfare_reward` where `activityId`=?";

    public Map<Integer, NewServerWelfareReward> getNewServerWelfareReward(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, NewServerWelfareReward> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getNewServerWelfareRewardSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();

            while (rs.next()) {
                NewServerWelfareReward newServerWelfareReward = parseData(rs);
                map.put(newServerWelfareReward.getConditionId(), newServerWelfareReward);
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateNewServerWelfareRewardSql = "update `t_u_activity_new_server_welfare_reward` set rewardRemain=? where activityId=? and conditionId=?";

    @Override
    protected boolean updateData(NewServerWelfareReward newServerWelfareReward) {
        newServerWelfareReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getRewardRemain()));
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getConditionId()));
        if (getDb().executeNoneQuery(updateNewServerWelfareRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertNewServerWelfareRewardSql = "insert into `t_u_activity_new_server_welfare_reward`(activityId,conditionId,rewardRemain) values (?,?,?)";

    @Override
    protected boolean addData(NewServerWelfareReward newServerWelfareReward) {
        newServerWelfareReward.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getConditionId()));
        para.put(i++, new DbParameter(Types.INTEGER, newServerWelfareReward.getRewardRemain()));
        if (getDb().executeNoneQuery(insertNewServerWelfareRewardSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected NewServerWelfareReward parseData(ResultSet rs) throws SQLException {
        NewServerWelfareReward newServerWelfareReward = new NewServerWelfareReward();
        newServerWelfareReward.setActivityId(rs.getInt("activityId"));
        newServerWelfareReward.setConditionId(rs.getInt("conditionId"));
        newServerWelfareReward.setRewardRemain(rs.getInt("rewardRemain"));

        return newServerWelfareReward;
    }

}
